//Source file: D:\\WTK104\\apps\\Toddtris\\src\\com\\toddsoft\\toddtris\\midlet\\ToddtrisStorage.java

package com.toddsoft.toddtris.midlet;

import com.toddsoft.util.ByteArrayHandler;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class ToddtrisStorage {
    private static final int SENSITIVITY_RECORD_ID = 1;
    private static final int KEYS_RECORD_ID = 2;
    private static final int WELL_STATE_RECORD_ID = 3;
    private static final int MARATHON_HIGH_SCORE_RECORD_ID = 4;
    private static final int TWENTY_FIVE_LINES_HIGH_SCORE_RECORD_ID = 5;
    private static final int FORTY_LINES_HIGH_SCORE_RECORD_ID = 6;
    private static final int SHOW_GRID_RECORD_ID = 7;
    private static final int NUM_RECORDS = SHOW_GRID_RECORD_ID;
    private static ToddtrisStorage instance = new ToddtrisStorage();
    private RecordStore rs;
    
    private ToddtrisStorage() {
        try {
            rs = RecordStore.openRecordStore("ToddtrisStorage", true);
            if (rs.getNumRecords() < NUM_RECORDS) {

                // add any required records
                byte[] bytes = " ".getBytes();
                int addRecords = NUM_RECORDS - rs.getNumRecords();
                for (int i = 0; i < addRecords; i++) {
                    rs.addRecord(bytes, 0, bytes.length);
                }
            }
        } catch (RecordStoreException rse) {
        }     
    }
    
    /**
     * @return ToddtrisStorage
     */
    public static ToddtrisStorage getInstance() {
        return instance;     
    }
    
    /**
     * @param position
     * @param val
     * @throws javax.microedition.rms.RecordStoreException
     */
    private void setInt(int position, int val) throws RecordStoreException {
        setIntArray(position, new int[]{val});
    }
    
    /**
     * @param position
     * @param defaultVal
     * @return int
     */
    private int getInt(int position, int defaultVal) {
        int[] ia = getIntArray(position);
        return (ia.length == 0) ? defaultVal : ia[0];
    }
    
    /**
     * @param position
     * @param val
     * @throws javax.microedition.rms.RecordStoreException
     */
    private void setIntArray(int position, int[] val) throws RecordStoreException {
        if (rs == null) {
            return;
        }
        ByteArrayHandler bah = new ByteArrayHandler(val.length * 4);
        for (int i = 0; i < val.length; i++) {
            bah.addInt(val[i]);
        }
        byte[] bytes = bah.getBytes();
        rs.setRecord(position, bytes, 0, bytes.length);     
    }
    
    /**
     * @param position
     * @return int[]
     */
    private int[] getIntArray(int position) {
        if (rs == null) {
            /* couldn't find record store */
            return new int[] {};
        }
        try {
            byte[] bytes = rs.getRecord(position);
            ByteArrayHandler bah = new ByteArrayHandler(bytes);
            int[] ints = new int[bytes.length / 4];
            for (int i = 0; i < ints.length; i++) {
                ints[i] = bah.getInt();
            }
            return ints;
        } catch (Exception e) {
            return new int[] {};
        }     
    }
    
    /**
     * @param position
     * @param val
     * @throws javax.microedition.rms.RecordStoreException
     */
    private void setBoolean(int position, boolean val) throws RecordStoreException {
        if (rs == null) {
            return;
        }
        ByteArrayHandler bah = new ByteArrayHandler();
        bah.addBoolean(val);
        byte[] bytes = bah.getBytes();
        rs.setRecord(position, bytes, 0, bytes.length);     
    }
    
    /**
     * @param position
     * @return boolean
     */
    private boolean getBoolean(int position) {
        if (rs == null) {
            /* couldn't find record store */
            return false;
        }
        try {
            byte[] bytes = rs.getRecord(position);
            ByteArrayHandler bah = new ByteArrayHandler(bytes);
            return bah.getBoolean();
        } catch (Exception e) {
            return false;
        }     
    }
    
    /**
     * @param bytes
     * @throws javax.microedition.rms.RecordStoreException
     */
    void setMarathonHighScores(byte[] bytes) throws RecordStoreException {
        rs.setRecord(MARATHON_HIGH_SCORE_RECORD_ID, bytes, 0, bytes.length);     
    }
    
    /**
     * @return byte[]
     */
    byte[] getMarathonHighScores() {
        try {
            return rs.getRecord(MARATHON_HIGH_SCORE_RECORD_ID);
        } catch (Exception e) {
            return new byte[] {};
        }     
    }

    /**
     * @param bytes
     * @throws javax.microedition.rms.RecordStoreException
     */
    void setTwentyFiveLineHighScores(byte[] bytes) throws RecordStoreException {
        rs.setRecord(TWENTY_FIVE_LINES_HIGH_SCORE_RECORD_ID, 
                     bytes,
                     0, 
                     bytes.length);     
    }
    
    /**
     * @return byte[]
     */
    byte[] getTwentyFiveLineHighScores() {
        try {
            return rs.getRecord(TWENTY_FIVE_LINES_HIGH_SCORE_RECORD_ID);
        } catch (Exception e) {
            return new byte[] {};
        }     
    }

    /**
     * @param bytes
     * @throws javax.microedition.rms.RecordStoreException
     */
    void setForyLineHighScores(byte[] bytes) throws RecordStoreException {
        rs.setRecord(FORTY_LINES_HIGH_SCORE_RECORD_ID, 
                     bytes,
                     0, 
                     bytes.length);     
    }
    
    /**
     * @return byte[]
     */
    byte[] getFortyLineHighScores() {
        try {
            return rs.getRecord(FORTY_LINES_HIGH_SCORE_RECORD_ID);
        } catch (Exception e) {
            return new byte[] {};
        }     
    }

    /**
     * @param sens
     * @throws javax.microedition.rms.RecordStoreException
     */
    void setSensitivity(int sens) throws RecordStoreException {
        setInt(SENSITIVITY_RECORD_ID, sens);     
    }
    
    /**
     * @param defaultSens
     * @return int
     */
    int getSensitivity(int defaultSens) {
        int sens = getInt(SENSITIVITY_RECORD_ID, defaultSens);
        return sens;     
    }
    
    /**
     * @param keys
     * @throws javax.microedition.rms.RecordStoreException
     */
    void setKeys(int[] keys) throws RecordStoreException {
        setIntArray(KEYS_RECORD_ID, keys);     
    }
    
    /**
     * @return int[]
     */
    int[] getKeys() {
        int[] keys = getIntArray(KEYS_RECORD_ID);
        return keys;     
    }
    
    /**
     * @param bytes
     * @throws javax.microedition.rms.RecordStoreException
     */
    void setWellState(byte[] bytes) throws RecordStoreException {
        rs.setRecord(WELL_STATE_RECORD_ID, bytes, 0, bytes.length);     
    }
    
    /**
     * @return byte[]
     */
    byte[] getWellState() {
        try {
            return rs.getRecord(WELL_STATE_RECORD_ID);
        } catch (Exception e) {
            return new byte[] {};
        }     
    }

    /**
     * @param state
     * @throws javax.microedition.rms.RecordStoreException
     */
    void setGridShowing(boolean state) throws RecordStoreException {
        setBoolean(SHOW_GRID_RECORD_ID, state);     
    }
    
    /**
     * @return boolean
     */
    boolean getGridShowing() {
        return getBoolean(SHOW_GRID_RECORD_ID);
    }
}
